package bit.mybatisdemo.demos.mapper;

import bit.mybatisdemo.demos.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
   // @Select("select id,username,`password`,age,gender,phone,delete_flag as deleteFlag"
    // +",create_time as createTime,update_time as updateTime from user_info")
    @Results(id="haha",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })

    @Select("select * from user_info")
    List<UserInfo> selectAll();

@ResultMap("haha")
    @Select("select * from user_info where id=${id}")
    UserInfo selectAllById(Integer id);

    @Select("select * from user_info where gender=1")
    List<UserInfo> selectAll2();

//    @Select("select * from user_info where gender=1")
//    UserInfo selectAll3();
//    只有list才能接收多个参数，否则会报错


//    @Select("select * from `user_info` where username=#{param1} and password=#{param2}")
     // @Select("select * from `user_info` where username=#{username} and password=#{password}")
      @Select("select * from `user_info` where username='${username}' and password='${password}'")
   List<UserInfo> selectByNameAndPassword(@Param("username") String username,@Param("password") String password);
//@Select("select * from `user_info` where username=#{username} and password=#{password}")
    //下面和上面一行加在一起会报错Parameter 'username' not found. Available parameters are [arg1, arg0, param1, param2]
    //List<UserInfo> selectByNameAndPassword( String username, String password);

    @Select("select * from `user_info` where username=#{username} and password=#{password}")
    List<UserInfo>selectByNameAndPassword2(String username,String password);

    //下方红紫色的名称必须是定义好的，否则spring会不知道它对应的数据库是哪个字段，会报错
    @Select("select * from user_info where username='${name00}'")
    List<UserInfo> selectByName(String name);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age) values(#{username},#{password},#{age})")
    Integer insertUser(UserInfo userInfo);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age) values(#{u.username},#{u.password},#{u.age})")
    Integer insertUser2(@Param("u")UserInfo userInfo);

    @Delete("delete from user_info where id=#{id}")
    Integer deleteById(Integer id);

    @Update("update user_info set delete_flag=#{deleteFlag},phone=#{phone} where id=#{id}")
    Integer updataUser(UserInfo userInfo);

    @Select("select * from user_info order by id ${a}")
    List<UserInfo> selectById(String a);

    @Select("select * from user_info where username like '%${username}%'")
    List<UserInfo> selectByLike(String username);

    @Select("select * from user_info where username like concat('%','username','%')")
    List<UserInfo> selectByLike2(String username);
}
